﻿Public Class EntregaDAO

    Public Shared Function insertarEntrega(ByVal CronogramaPresentacionId As Integer, ByVal EntregaFechaLimite As DateTime, ByVal EntregaNumero As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_Entrega"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.INSERTAR).DbType = DbType.Int32
                    .AddWithValue("@EntregaId", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@CronogramaPresentacionId", CronogramaPresentacionId).DbType = DbType.Int32
                    .AddWithValue("@EntregaFechaLimite", EntregaFechaLimite).DbType = DbType.DateTime
                    .AddWithValue("@EntregaNumero", EntregaNumero).DbType = DbType.Int32
                End With
                .Connection = cn
                .ExecuteNonQuery()
            End With
            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function obtenerEntregas(ByVal EntregaId As Integer) As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_Entrega_obtener", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@EntregaId", EntregaId).DbType = DbType.Int32
            da.Fill(ds, "Entregas")

            Return ds.Tables("Entregas")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function actualizarEntrega(ByVal EntregaId As Integer, ByVal CronogramaPresentacionId As Integer, ByVal EntregaFechaLimite As DateTime, ByVal EntregaNumero As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_Entrega"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.ACTUALIZAR).DbType = DbType.Int32
                    .AddWithValue("@EntregaId", EntregaId).DbType = DbType.Int32
                    .AddWithValue("@CronogramaPresentacionId", CronogramaPresentacionId).DbType = DbType.Int32
                    .AddWithValue("@EntregaFechaLimite", EntregaFechaLimite).DbType = DbType.DateTime
                    .AddWithValue("@EntregaNumero", EntregaNumero).DbType = DbType.Int32
                End With
                .Connection = cn
                .ExecuteNonQuery()

            End With
            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function eliminarEntrega(ByVal EntregaId As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_Entrega"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.ELIMINAR).DbType = DbType.Int32
                    .AddWithValue("@EntregaId", EntregaId).DbType = DbType.Int32
                    .AddWithValue("@CronogramaPresentacionId", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@EntregaFechaLimite", Constante.FECHAVACIA).DbType = DbType.DateTime
                    .AddWithValue("@EntregaNumero", Constante.MENOSUNO).DbType = DbType.Int32
                End With
                .Connection = cn
                .ExecuteNonQuery()
            End With

            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function obtenerEntregasDeCronogramaPresentacion(ByVal CronogramaPresentacionId As Integer) As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_entrega_cronogramaPresentacionId", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@CronogramaPresentacionId", CronogramaPresentacionId).DbType = DbType.Int32
            da.Fill(ds, "Entregas")

            Return ds.Tables("Entregas")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function obtenerDetallesDeCronograma(ByVal CronogramaPresentacionId As Integer, ByVal FarmaciaId As Integer) As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_detalleRecepcion_noCargados", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@CronogramaPresentacionId", CronogramaPresentacionId).DbType = DbType.Int32
            da.SelectCommand.Parameters.AddWithValue("@FarmaciaId", FarmaciaId).DbType = DbType.Int32
            da.Fill(ds, "Entregas")

            Return ds.Tables("Entregas")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function obtenerEntregasDeCronogramaPresentacionDs(ByVal CronogramaPresentacionId As Integer) As DataSet
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_obtenerDetallesCronograma", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@CronogramaPresentacionId", CronogramaPresentacionId).DbType = DbType.Int32
            da.Fill(ds, "Entregas")
            Return ds
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function idEntrega(ByVal CronogramaPresentacionId As Integer, ByVal EntregaNumero As Integer) As Integer
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim cmd As New SqlClient.SqlCommand("usp_entrega_id", cn)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@CronogramaPresentacionId", CronogramaPresentacionId).DbType = DbType.Int32
            cmd.Parameters.AddWithValue("@EntregaNumero", EntregaNumero).DbType = DbType.Int32
            Dim id As Integer
            id = cmd.ExecuteScalar
            Return id
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function obtenerDetalles(ByVal EntregaId As Integer) As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_entrega_detallesEntrega", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@EntregaId", EntregaId).DbType = DbType.Int32
            da.Fill(ds, "DetallesEntregas")

            Return ds.Tables("DetallesEntregas")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

End Class
